package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.system.mapper.SysSalaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysSalary;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysSalaryService;

/**
 * 薪资信息管理Controller
 *
 * @author Kelsey
 */
@RestController
@RequestMapping("/system/salary")
public class SysSalaryController extends BaseController {

    @Autowired
    private ISysSalaryService salaryService;

    @Autowired
    private SysSalaryMapper salaryMapper;

    /**
     * 查询薪资信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:salary:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSalary salary) {
        IPage<SysSalary> list = salaryMapper.selectSalaryList(getPage(),salary);
        return getDataTable(list);
    }

    /**
     * 导出薪资信息列表
     */
    @Log(title = "薪资管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:salary:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSalary salary) {
        List<SysSalary> list = salaryService.selectSalaryList(salary);
        ExcelUtil<SysSalary> util = new ExcelUtil<SysSalary>(SysSalary.class);
        util.exportExcel(response, list, "薪资数据");
    }

    /**
     * 获取薪资信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:salary:query')")
    @GetMapping(value = "/{salaryId}")
    public AjaxResult getInfo(@PathVariable("salaryId") Long salaryId) {
        return success(salaryService.selectSalaryById(salaryId));
    }

    /**
     * 修改保存薪资信息
     */
    @PreAuthorize("@ss.hasPermi('system:salary:edit')")
    @Log(title = "薪资管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysSalary salary) {
        // 校验该用户在该月份是否存在薪资信息
        if (!salaryService.checkSalaryExist(salary)) {
            return AjaxResult.error("修改薪资失败，该用户" + salary.getUserId() + "在" + salary.getSalaryMonth() + "已存在薪资信息");
        }
        salary.setUpdateBy(getUsername());
        return toAjax(salaryService.updateSalary(salary));
    }

    /**
     * 删除薪资信息
     */
    @PreAuthorize("@ss.hasPermi('system:salary:remove')")
    @Log(title = "薪资管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{salaryIds}")
    public AjaxResult remove(@PathVariable Long[] salaryIds) {
        return toAjax(salaryService.deleteSalaryByIds(salaryIds));
    }

    /**
     * 新增保存薪资信息
     */
    @PreAuthorize("@ss.hasPermi('system:salary:add')")
    @Log(title = "薪资管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysSalary salary) {
        // 校验该用户在该月份是否存在薪资信息
        if (!salaryService.checkSalaryExist(salary)) {
            return AjaxResult.error("新增薪资失败，该用户" + salary.getUserId() + "在" + salary.getSalaryMonth() + "已存在薪资信息");
        }
        // 初始化薪资信息
        salary.setStatus("0");
        salary.setCreateBy(getUsername());
        return toAjax(salaryService.insertSalary(salary));
    }
} 